﻿Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting

<TestClass()> Public Class UnitTest1

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = Value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    ' <ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    ' End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    <TestMethod()> Public Sub Teste_De_Regular_Expression()

        Teste.RegexExpressions.TestRegularExpressions.Main()

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Teste_Regular_Expression_Validation()

        Teste.RegexExpressions.TestRegularExpressionValidation.Main()

        Assert.IsTrue(True)

    End Sub


    <TestMethod()> Public Sub Substituir_Template()

        Dim _template As String = "O usuário ##USERNAME## de e-mail ##EMAIL## "
        Dim _dicionario As New Dictionary(Of String, String)
        Dim _parseado As String

        _dicionario.Add("##USERNAME##", "lsantana")
        _dicionario.Add("##EMAIL##", "leopororoca@gmail.com")

        _parseado = Me.SimpleTemplate(_template, _dicionario)

        Console.WriteLine(_parseado)

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Detecting_Error_Code()

        Dim regexPattern As String = ": error BC(.*?):"
        Dim _errorLine As String = "\Teste.SigaDois.Servicos\Servicos_SigaDois\S2AcademicoServicoTeste.vb(347): error BC30002: O tipo 'Fgv.SigaDois.Servicos.SistemasSatelites.CertificadoEmitidoDto' nÒo estß definido."
        Dim tagName As String
        Dim tagMatches As RegularExpressions.MatchCollection = RegularExpressions.Regex.Matches(_errorLine, regexPattern)

        For Each match As RegularExpressions.Match In tagMatches
            tagName = match.ToString()
            Console.WriteLine("{0}", tagName)
            tagName = tagName.Replace(": error ", "").Replace(":", "")
            Console.WriteLine("{0}", tagName)
        Next

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Detecting_Linha_DoErro()

        Dim regexPattern As String = "\(.*?\)"
        Dim _errorLine As String = "\Teste.SigaDois.Servicos\Servicos_SigaDois\S2AcademicoServicoTeste.vb(347): error BC30002: O tipo 'Fgv.SigaDois.Servicos.SistemasSatelites.CertificadoEmitidoDto' nÒo estß definido."
        Dim tagName As String
        Dim tagMatches As RegularExpressions.MatchCollection = RegularExpressions.Regex.Matches(_errorLine, regexPattern)

        For Each match As RegularExpressions.Match In tagMatches
            tagName = match.ToString()
            Console.WriteLine("{0}", tagName)
            tagName = tagName.Replace("(", "").Replace(")", "")
            Console.WriteLine("{0}", tagName)
        Next

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Detecting_Pagina_Do_Erro()

        Dim regexPattern As String = ".*?\.vb"
        Dim _errorLine As String = "\Teste.SigaDois.Servicos\Servicos_SigaDois\S2AcademicoServicoTeste.vb(347): error BC30002: O tipo 'Fgv.SigaDois.Servicos.SistemasSatelites.CertificadoEmitidoDto' nÒo estß definido."
        Dim tagName As String
        Dim tagMatches As RegularExpressions.MatchCollection = RegularExpressions.Regex.Matches(_errorLine, regexPattern)

        For Each match As RegularExpressions.Match In tagMatches
            tagName = match.ToString()
            Console.WriteLine("{0}", tagName)
            tagName = tagName.Replace("(", "").Replace(")", "")
            Console.WriteLine("{0}", tagName)
        Next

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Detecting_Mensagem_Erro()

        Dim regexPattern As String = ":\s.*"
        Dim _errorLine As String = "\Teste.SigaDois.Servicos\Servicos_SigaDois\S2AcademicoServicoTeste.vb(347): error BC30002: O tipo 'Fgv.SigaDois.Servicos.SistemasSatelites.CertificadoEmitidoDto' nÒo estß definido."
        Dim tagName As String
        Dim tagMatches As RegularExpressions.MatchCollection = RegularExpressions.Regex.Matches(_errorLine, regexPattern)

        For Each match As RegularExpressions.Match In tagMatches
            tagName = match.ToString()
            Console.WriteLine("{0}", tagName)
            tagName = tagName.Replace(": error ", "")
            Console.WriteLine("{0}", tagName)
        Next

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Detecting_Fully_Qualified_Error()

        Dim regexPattern As String = "\'\w.*\'"
        Dim _errorLine As String = "\Teste.SigaDois.Servicos\Servicos_SigaDois\S2AcademicoServicoTeste.vb(347): error BC30002: O tipo 'Fgv.SigaDois.Servicos.SistemasSatelites.CertificadoEmitidoDto' nÒo estß definido."
        Dim tagName As String
        Dim tagMatches As RegularExpressions.MatchCollection = RegularExpressions.Regex.Matches(_errorLine, regexPattern)

        For Each match As RegularExpressions.Match In tagMatches
            tagName = match.ToString()
            Console.WriteLine("{0}", tagName)
            tagName = tagName.Replace("'", "")
            Console.WriteLine("{0}", tagName)
        Next

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Detecting_Palavras_6_Letras()

        Dim regexPattern As String = "\b\w{5,6}\b"
        Dim _errorLine As String = "laranja manga limão jaca amora castanha cupuaçu açai mangaba pitanga amora caju sapoti jaboticaba caja banana graviola"
        Dim tagName As String
        Dim tagMatches As RegularExpressions.MatchCollection = RegularExpressions.Regex.Matches(_errorLine, regexPattern)

        For Each match As RegularExpressions.Match In tagMatches
            tagName = match.ToString()
            Console.WriteLine("{0}", tagName)
            tagName = tagName.Replace("'", "")
            Console.WriteLine("{0}", tagName)
        Next

        Assert.IsTrue(True)

    End Sub



    Private Function SimpleTemplate(ByVal template As String, ByVal replacements As Dictionary(Of String, String)) As String
        ' parse the message into an array of tokens
        Dim regex As New RegularExpressions.Regex("(##[^#]+##)")
        Dim tokens As String() = regex.Split(template)

        ' the new message from the tokens
        Dim sb = New StringBuilder(CInt(Math.Truncate(CDbl(template.Length) * 1.1)))
        For Each token As String In tokens
            sb.Append(If(replacements.ContainsKey(token), replacements(token), token))
        Next

        Return sb.ToString()
    End Function

    <TestMethod()> Public Sub Substituir_Tags()

        Dim _texto As String = "Aqui tenho um texto de levantamento de requisito. Meu nome é {zeus:nome}. Minha altura é {zeus:altura}. Tenho um segredo que é {zeus:segredo}"
        Dim _interpretado As String

        _interpretado = Me.ParseTagsFromPage(_texto)

        Console.WriteLine(_interpretado)

        Assert.IsTrue(True)

    End Sub

    Private Function ParseTagsFromPage(ByVal pageContent As String) As String
        Dim regexPattern As String = "{zeus:(.*?)}"
        'matches {zeus:anytagname}
        Dim tagName As String = ""
        Dim fieldName As String = ""
        Dim replacement As String = ""
        Dim tagMatches As RegularExpressions.MatchCollection = RegularExpressions.Regex.Matches(pageContent, regexPattern)
        For Each match As RegularExpressions.Match In tagMatches
            tagName = match.ToString()
            fieldName = tagName.Replace("{zeus:", "").Replace("}", "")
            'get data based on my found field name, using some other function call
            replacement = GetFieldValue(fieldName)
            pageContent = pageContent.Replace(tagName, replacement)
        Next
        Return pageContent
    End Function

    Private Function GetFieldValue(ByVal fieldName As String) As String
        Dim _dicionario As New Dictionary(Of String, String)
        With _dicionario
            .Add("altura", "1,85m")
            .Add("peso", "80Kg")
            .Add("nome", "Leonardo Santana")
        End With

        If _dicionario.Keys.Contains(fieldName) Then
            Return _dicionario(fieldName)
        Else
            Return String.Format("{{zeus:{0}}}", fieldName)
        End If

    End Function



End Class
